package com.hotel.couponsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.status.ResultMsg;
import com.hotel.couponsystem.mapper.CouponStockMapper;
import com.hotel.couponsystem.pojo.CouponStock;
import com.hotel.couponsystem.service.CouponStockService;
import com.hotel.hotelsystem.pojo.HotelList;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * (CouponStock)表服务实现类
 *
 * @author makejava
 * @since 2022-09-30 15:32:29
 */
@Service("couponStockService")
public class CouponStockServiceImpl extends ServiceImpl<CouponStockMapper,CouponStock> implements CouponStockService {

    @Resource
    private CouponStockMapper couponStockMapper;

    // 注销优惠券记录
    @Override
    public Boolean removeByCouponNo(Integer couponNo) {
        int row = couponStockMapper.deleteByCouponNo(couponNo);
        return row>0;
    }

    // 查询单条记录
    @Override
    public CouponStock getByCouponNo(Integer couponNo) {
        CouponStock stock = couponStockMapper.selectByCouponNo(couponNo);
        return stock;
    }

    // 条件查询
    @Override
    public List<CouponStock> getListByCondition(String couponName, String couponNo, String couponStatusNo) {
        LambdaQueryWrapper<CouponStock> lbq = new LambdaQueryWrapper<>();
        // 模糊查询
        lbq.like(Strings.isNotEmpty(couponName),CouponStock::getCouponName,couponName)
                .like(Strings.isNotEmpty(couponNo),CouponStock::getCouponNo,couponNo)
                .like(Strings.isNotEmpty(couponStatusNo),CouponStock::getCouponStatusNo,couponStatusNo);
        List<CouponStock> list = couponStockMapper.selectList(lbq);
        return list;
    }

    // 查询优惠券适用方的编号
    @Override
    public List<CouponStock> getListCouponByHotelNo(Integer couponStatusNo) {
        List<CouponStock> list = couponStockMapper.selectByCouponStatusNo(couponStatusNo);
        return list;
    }

    // 减少优惠券库存 -1
    @Override
    public String reduceCouponStock(String couponNo) {
        // 先查询该优惠券记录
        CouponStock stock = getByCouponNo(Integer.valueOf(couponNo));
        // 判断库存量是否>0
        if (stock.getStock() > 0) {
            Integer row = couponStockMapper.reduceCouponStock(couponNo,stock.getVersion());
            if (row>0) return ResultMsg.SECKIL_COUPON_SUCCESS;
            else return ResultMsg.SECKIL_COUPON_FAIL;
        } else return ResultMsg.COUPON_STOCK_NULL;
    }

    //优惠券的分页查询
    @Override
    public IPage<CouponStock> selectByPage(Integer currentPage,Integer pageSize) {
        Page<CouponStock> page=new Page<>(currentPage,pageSize);
        System.out.println(page.getSize());
        IPage<CouponStock> page2 = couponStockMapper.selectPage(page,null);
        return page2;
    }

}
